import { _decorator, Component, Node, Vec3, v3, v2, Sprite } from 'cc';
import { BasicComponet } from '../../Init/Basic/BasicComponet';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import GlobalPool from '../../Init/Tools/GlobalPool';
import { BasicEnemy } from './BasicEnemy';
import { Zombie2D } from './Zombie2D';
const { ccclass, property } = _decorator;

@ccclass('Missile2D')
export class Missile2D extends BasicComponet {
    atk = 0;
    lineVec = v3();
    toPos = v3();
    isFinish = false;
    sumDist = 0;
    moveVec = v3();
    atkRadius = GlobalConfig.Missile.atkRadius;
    maxHight = 0; //最大飞行高度

    setData(d: { p: Vec3, toP: Vec3, atk: number, t: number }) {
        this.node.setPosition(d.p);
        this.toPos.set(d.toP);
        this.lineVec.set(d.toP).subtract(d.p);
        this.sumDist = this.lineVec.length();
        this.maxHight = this.sumDist / 200;
        this.lineVec.normalize().multiplyScalar(GlobalConfig.Missile.spd);
        this.atk = d.atk;
        this.moveVec.set(0);
        this.isFinish = false;
        //同步显示3d
        this.node.active = true;
        this.node[GlobalEnum.ExtralProp.curLineVec2d] = v2(this.lineVec.x, this.lineVec.y);
        // this.node[GlobalEnum.ExtralProp.jumpHight] = 1;
        this.node[GlobalEnum.ExtralProp.moveProgress] = 0;

        //
        let sp = this.node.getComponent(Sprite);
        if (sp) {
            sp.enabled = GlobalConfig.isShow2dCollider;
        }
    }
    tmpV = v3();
    update(dt) {
        if (this.isFinish || GlobalTmpData.Game.isGamePause) return;
        this.tmpV.set(this.lineVec).multiplyScalar(dt);
        this.moveVec.add(this.tmpV);
        //位移
        this.node.setPosition(this.tmpV.add(this.node.position));
        let curDist = this.moveVec.length();
        //计算3d高度
        // let rate = Math.sin((curDist / this.sumDist) * 3.14);
        // let h = rate * this.maxHight + (1 - rate) * 1.5;
        // this.node[GlobalEnum.ExtralProp.jumpHight] = h / GlobalConfig.Scale2DTo3D;

        if (curDist >= this.sumDist) {
            this.isFinish = true;
            this.onBoom();
            GlobalPool.put(this.node);
        }
    }
    enemyArr = [GlobalEnum.EnemyTypes.People];
    _tmpP = v3();
    onBoom() {
        AudioSystem.playEffect(AudioEnum.Boom);
        //检测附近平民、僵尸
        let outEnemy: { [t: number]: BasicEnemy } = {};
        this.emit(EventTypes.EnemyEvents.CheckEnemyByType,
            { p: this.node.position, r: this.atkRadius, types: this.enemyArr, out: outEnemy })
        for (const key in outEnemy) {
            const e = outEnemy[key];
            e.byAtk(false);
        }
        let outZombies: Zombie2D[] = [];
        this.emit(EventTypes.EnemyEvents.CheckZombies, { wp: this.node.worldPosition, r: this.atkRadius, outZombies: outZombies });
        for (let i = 0; i < outZombies.length; i++) {
            outZombies[i].byAtk(this.atk);
        }

        //爆炸效果
        this._tmpP.x = this.node.position.x * GlobalConfig.Scale2DTo3D;
        this._tmpP.z = -this.node.position.y * GlobalConfig.Scale2DTo3D;
        this._tmpP.y = 1;
        EventManager.emit(EventTypes.EffectEvents.show2DEffect, { t: GlobalEnum.Effect2DType.TankShootBoom, p: this._tmpP });
    }

}

